using System;
using Microsoft.Win32;

namespace extend_360.utils
{
    public class RegistryKeyUtils
    {
        /// <summary>
        /// 获取注册表信息
        /// </summary>
        /// <param name="root"></param>
        /// <param name="keyName"></param>
        /// <returns></returns>
        public static Tuple<bool, string> GetRegistryValue(RegistryKey root, string keyName,
            string softName)
        {
            try
            {
                string[] registryPaths = new[]
                {
                    @"SOFTWARE\WOW6432Node\Microsoft\Windows\CurrentVersion\Uninstall",
                    @"SOFTWARE\Microsoft\Windows\CurrentVersion\Uninstall"
                };
                foreach (var registryPath in registryPaths)
                {
                    using (RegistryKey key = root.OpenSubKey(registryPath))
                    {
                        if (key != null)
                        {
                            foreach (string subKeyName in key.GetSubKeyNames())
                            {
                                using (RegistryKey subKey = key.OpenSubKey(subKeyName))
                                {
                                    if (subKey != null && subKey.GetValue("DisplayName")?.ToString()
                                            ?.ToLower()
                                        == softName.ToLower())
                                    {
                                        return new Tuple<bool, string>(true,
                                            subKey.GetValue(keyName).ToString());
                                    }
                                }
                            }
                        }
                    }
                }
            }
            catch (Exception ex)
            {
            }

            return new Tuple<bool, string>(false, string.Empty);
        }

        /// <summary>
        /// 从注册表中获取数据 存在获取 不存在写
        /// </summary>
        /// <param name="registryPath"></param>
        /// <param name="key"></param>
        /// <param name="value"></param>
        /// <returns></returns>
        public static string GetRegistryValue(string registryPath, string key, string value)
        {
            RegistryKey registryKey = Registry.LocalMachine.OpenSubKey(registryPath);
            if (registryKey == null)
                registryKey = Registry.LocalMachine.CreateSubKey(registryPath);
            string v = registryKey?.GetValue(key)?.ToString();
            if (registryKey != null && string.IsNullOrEmpty(v))
            {
                v = value;
                registryKey.SetValue(key, value);
            }

            SystemUtils.ConsoleWrite($"随机目录信息==={v}", ConsoleColor.Green);
            return v;
        }

        /// <summary>
        /// 检查注册表是否存在
        /// </summary>
        /// <param name="registryPath"></param>
        /// <param name="key"></param>
        /// <returns></returns>
        public static bool CheckRegistryExists(string registryPath, string key)
        {
            RegistryKey registryKey = Registry.LocalMachine.OpenSubKey(registryPath);
            if (registryKey == null)
                return false;
            string v = registryKey?.GetValue(key)?.ToString();
            if (string.IsNullOrEmpty(v))
                return false;
            return true;
        }
    }
}